﻿layui.use(['form', 'table', 'layer'], function () {
    var $ = layui.$,
        form = layui.form,
        table = layui.table,
        layer = layui.layer;

    //初始化文章表格
    var tableaccessory = table.render({
        elem: '#tableaccessoryoss',
        url: '/Ashx/article.ashx', //数据接口
        method: 'post',
        where: { functionname: "ossjhyyeye", pre: "", del: "/" },
        cols: [[ //表头 
            { type: "checkbox" },
            { field: 'key', title: '名称', event: "show" },
            { field: 'size', title: '大小' }

        ]]
    });

    table.on("tool(tableaccessoryoss)", function (obj) {
        var data = obj.data;
        if (obj.event === "show") {//预览图片     
            var key = data.key;
            if (key.lastIndexOf(".") == -1) {
                $("#osspath").text(key);
                tableaccessory.reload({
                    where: { functionname: "ossjhyyeye", pre: key, del: "/" },
                })
            } else {
                var kzm = key.substring(key.lastIndexOf(".") + 1, key.length);
                if (kzm == "jpg" || kzm == "png" || kzm == "gif" || kzm == "jpeg" || kzm == "bmp") {
                    // window.open("https://jhyyeye.oss-cn-beijing.aliyuncs.com/" + key);
                    parent.layer.open({
                        type: 1,
                        title: "图片预览",
                        content: "<img src=https://jhyyeye.oss-cn-beijing.aliyuncs.com/" + key + " width='400' height='300' />",
                        //offset: 'b',
                        area: ['400px', '343px']
                    });
                } else {
                    parent.layer.msg("暂时只支持部分图片的预览");
                }
            }

        }
    });

    //返回操作
    $("#btn-back").click(function () {

      
        var osspath = $("#osspath").text();
        if (osspath.length > 1) {
            osspath = osspath.substring(0, osspath.length - 1);
            var pre = null;
            if (osspath.lastIndexOf("/") == -1) {
                pre = "";
            } else
            {
                pre = osspath.substring(0, osspath.lastIndexOf("/")+1);
            }
            $("#osspath").text(pre);
            tableaccessory.reload({
                where: { functionname: "ossjhyyeye", pre: pre, del: "/" },
            })
        } else
        {
            parent.layer.msg("现在已经是根目录了");
        }
    });

    //删除操作
    $("#btn-delete").click(function () {
        var checkStatus = table.checkStatus('tableaccessoryoss'),
            data = checkStatus.data;

        if (data.length < 1) {
            parent.layer.msg("还没有选择要删除的文件");
        } else
        {
            var n = 0;
            $.each(data, function (a,b) {

                $.ajax({
                    type: "post",
                    url: "/Ashx/article.ashx",
                    data: { functionname: "ossjhyyeyedel", key:b.key },
                    success: function (msg) {
                        if (msg=="true") {
                            n += 1;
                        }
                    }
                });
            });
            alert(n);
            tableaccessory.reload();


        }


    });

})